package com.poly.cms.controller;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.poly.cms.pojo.dto.CmsChannelDTO;
import com.poly.cms.pojo.query.CmsChannelQuery;
import com.poly.cms.pojo.vo.CmsChannelTreeVO;
import com.poly.cms.pojo.vo.CmsChannelVO;
import com.poly.cms.service.ICmsChannelService;
import com.poly.common.core.domain.R;
import com.poly.common.core.tools.utils.Func;
import com.poly.common.core.validate.Add;
import com.poly.common.core.validate.Update;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.log.annotation.Log;
import com.poly.common.log.enums.BusinessType;
import com.poly.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 频道信息Controller
 *
 * @author poly
 * @date 2023-10-25
 */
@Api(tags = "频道信息")
@RestController
@RequestMapping("/channel")
public class ChannelController extends BaseController {
    @Autowired
    private ICmsChannelService cmsChannelService;

    /**
     * 查询频道信息列表
     */
    @ApiOperation("查询频道信息树形列表")
    @RequiresPermissions("cms:channel:list")
    @GetMapping("/list")
    public R list(CmsChannelQuery query) {
        List<CmsChannelVO> channelVOS = cmsChannelService.listTree(query);
        return R.ok(channelVOS);
    }

    /**
     * 获取频道信息详细信息
     */
    @ApiOperation("获取频道信息详细信息")
    @RequiresPermissions("cms:channel:query")
    @GetMapping(value = "/{id}")
    public R<CmsChannelVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(cmsChannelService.getVO(id));
    }

    /**
     * 新增频道信息
     */
    @ApiOperation("新增频道信息")
    @Log(title = "频道管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("cms:channel:add")
    @PostMapping
    public R add(@RequestBody @Validated({Add.class}) CmsChannelDTO dto) {
        return toResult(cmsChannelService.add(dto));
    }

    /**
     * 修改频道信息
     */
    @ApiOperation("修改频道信息")
    @Log(title = "频道管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("cms:channel:edit")
    @PutMapping
    public R edit(@RequestBody @Validated({Update.class}) CmsChannelDTO dto) {
        return toResult(cmsChannelService.update(dto));
    }

    /**
     * 删除频道信息
     */
    @ApiOperation("删除频道信息")
    @Log(title = "频道管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("cms:channel:remove")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return toResult(cmsChannelService.deleteByIds(ids));
    }

    @GetMapping("/listTree")
    @ApiOperation("获取频道树")
    public R listTree(CmsChannelQuery qo) {
        List<CmsChannelVO> treeVOS = cmsChannelService.listTree(qo);
        CmsChannelTreeVO cmsChannelVO = new CmsChannelTreeVO();
        cmsChannelVO.setChildren(Func.copyProperties(treeVOS,CmsChannelTreeVO.class));
        cmsChannelVO.setChannelName("无");
        cmsChannelVO.setId(0L);
        List<CmsChannelTreeVO> convert1 = Lists.newArrayList();
        convert1.add(cmsChannelVO);
        return R.ok(convert1);
    }
}
